package com.yunban.project.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lijiajun
 * @date 2024/3/20
 */
@RestControllerAdvice
public class BaseExceptionAdvice {

    public Map<String, Object> handleExceptionResult(int code, String msg) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    // 统一异常处理
    @ExceptionHandler(BaseException.class)
    public Map<String, Object> handleBaseException(BaseException e) {
        return this.handleExceptionResult(e.getCode(), e.getMessage());
    }
}
